package com.voltmao.webpro.utils.wechat.app;

import com.voltmao.webpro.utils.StringUtils;
import com.voltmao.webpro.utils.http.HttpRequestUtil;
import com.voltmao.webpro.utils.http.HttpResponseUtils;
import com.voltmao.webpro.utils.wechat.IWechatUtils;
import com.voltmao.webpro.utils.wechat.utils.dto.msg.response.SendTxtMsg;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedList;
import java.util.List;

/**
 * 指令控制器
 */
public class CommandController implements ICommandController {

    List<CommandHandler> commandHandlerList = new LinkedList<>();
    private IWechatUtils wechatUtils;
    HttpServletResponse response;
    HttpServletRequest request;

    public CommandController(IWechatUtils wechatUtils, HttpServletRequest request, HttpServletResponse response) {
        this.wechatUtils = wechatUtils;
//        this.openId=openId;
        this.request = request;
        this.response = response;
    }

    /**
     * 路由条件(命令名相等)
     *
     * @param content
     */
    public void routeCondition(String content, String openId) {

        boolean isShowDes = true;
        String resultStr = "";

        for (CommandHandler item : commandHandlerList) {
            if (content.equals(item.getName())) {
                isShowDes = false;

                resultStr = item.responseMsg(wechatUtils, openId, this);
                break;
            }
        }

        if (isShowDes) {
//            this.showDesCommand.responseMsg(wechatUtils,openId);
            resultStr = showAllCommand();
        }

        if (StringUtils.isEmpty(resultStr)) {
            return;
        }

        SendTxtMsg sendTxtMsg = new SendTxtMsg();
        sendTxtMsg.setFromUserName(wechatUtils.getAccount().getOriginId());
        sendTxtMsg.setToUserName(openId);
        sendTxtMsg.setContent(resultStr);
        sendTxtMsg.setCreateTime(System.currentTimeMillis() + "");


        HttpResponseUtils.writerXml(response, wechatUtils.getMsgUtils().builderTxtMsg(sendTxtMsg));
    }

//    /**
//     * 路由所有条件(命令名相等)
//     * @param content
//     */
//    public void routeAllCondition(String content,String openId){
//
//        boolean isShowDes=true;
//
//        for(CommandHandler item: commandHandlerList)
//        {
//            if(content.equals(item.getName())){
//                item.responseMsg(wechatUtils,openId);
//
//                isShowDes=false;
//            }
//        }
//
//        if(isShowDes){
//            this.showDesCommand.responseMsg(wechatUtils,openId);
//        }
//    }


    /**
     * 所有命令描述
     *
     * @return
     */
    public String showAllCommand() {
        StringBuilder resultString = new StringBuilder();

        for (CommandHandler item : commandHandlerList) {
            if (item.getIsShow()) {
                resultString.append("回复:" + item.getName() + "=>" + item.getDes() + "\n");
            }
        }

        return resultString.toString();
    }

    /**
     * 监听命令
     *
     * @param commandHandler
     */
    public void addListener(CommandHandler commandHandler) {
        this.commandHandlerList.add(commandHandler);
    }

    @Override
    public String getRequestIp() {
        return HttpRequestUtil.getClientIpAddr(request);
    }
}
